﻿using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
using Microsoft.Extensions.Logging;
using ShopGrpcService.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ShopGrpcService.Services
{
    public class CategoriesService:Categories.CategoriesBase
    {
        private ShopContext shopdb;
        private ILogger<ProductService> logger;

        public CategoriesService(ShopContext shopContext, ILogger<ProductService> log)
        {
            shopdb = shopContext;
            logger = log;
        }
        public override Task<CategoriesListResponse> GetCategoriesList(Empty request, ServerCallContext context)
        {
            var res = new CategoriesListResponse();
            var cateList = shopdb.Categories.Select(p => new CategoriesInfo
            {
                CategoryId = p.CategoryId,
                CategoryName = p.CategoryName
            }).ToArray();
            logger.LogInformation($"以读取到商品数据:{cateList.Length}条");
            //foreach (var item in prolist)
            //{
            //    res.ProductInfos.Add(item);
            //}
            res.CategoryInfos.Add(cateList);
            return Task.FromResult(res);
        }

    }
}
